-- 轰烬朽徽
local hongjinxiuwei = fk.CreateSkill {
    name = "mcdh_hongjinxiuwei"
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

hongjinxiuwei:addEffect(fk.EventPhaseStart,{
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(self) and target.phase == Player.Play and target == player
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
            local  targets  =table.filter(room:getOtherPlayers(player),
                    function(p)
                        return #lib.getCanPublicCards(player)+#lib.getCanPublicCards(p) >= 3 and player:inMyAttackRange(p)
                    end )
            local res = room:askToChoosePlayers(player, {
                targets = targets,
                min_num = 1,
                max_num = 1,
                prompt = "#mcdh_hongjinxiuwei",
                skill_name = self.name,
                cancelable = true
              })
            if #res > 0  then
                self.cost_data = res[1]
                return true
            end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
            local to =self.cost_data
            local player_CanPublic = lib.getCanPublicCards(player)
            local to_CanPublic = lib.getCanPublicCards(to)
            local cards1 = {}
            if #player_CanPublic ~= 0  then
                cards1 = lib.askForPublicCard(room,player,player,3-#to_CanPublic,3,self.name,false,"#mcdh_hongjinxiuwei-public:"..to.id,".",false)
            end
            if 3-#cards1 ~= 0 then
                lib.askForPublicCard(room,player,to,3-#cards1,3-#cards1,self.name,false,"#mcdh_hongjinxiuwei-public:"..to.id,".",false)
            end
    end
})

hongjinxiuwei:addEffect(fk.TurnEnd,{
    can_trigger = function(self, event, target, player, data)
       if player:hasSkill(self)  then
            local room = player.room
            local logic = room.logic
            local events =logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
                local overt_cards = {}
                if e.data.extra_data and e.data.extra_data.overt_cards then
                    overt_cards = e.data.extra_data.overt_cards
                end
                for _, move in ipairs(e.data) do
                    if move.from then
                        for _, info in ipairs(move.moveInfo) do
                            if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) and table.contains(overt_cards,info.cardId) then
                                return true
                            end
                        end
                    end
                end
            end, Player.HistoryTurn)
            return #events == 1
        end
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
            return room:askForSkillInvoke(player, self.name, data)
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local success, dat = room:askForUseActiveSkill(player, "mcdh_hongjinxiuwei_viewas", "#mcdh_hongjinxiuwei-use", true)
        if dat then
                local card = Fk:cloneCard("fire__slash")
                card:addSubcards(dat.cards)
                card.skillName = self.name
                room:useCard{
                    from = player,
                    tos = dat.targets,
                    card = card,
                }
        else
                player:drawCards(1, self.name)
        end
    end
})

Fk:loadTranslationTable {
    ["mcdh_hongjinxiuwei"] = "轰烬朽徽",
    [":mcdh_hongjinxiuwei"] = "出牌阶段开始时，你可以明置你与一名在你攻击范围内的角色共计三张牌；每回合结束时，若本回合有角色失去过明置牌，你可以摸一张牌或将一张牌当火【杀】使用。",
    ["#mcdh_hongjinxiuwei-use"] = "将一张牌当火【杀】使用，否则摸一张牌",
    ["#mcdh_hongjinxiuwei-public"] = "明置你与%src共计三张牌",
    ["#mcdh_hongjinxiuwei"] = "轰烬朽徽: 明置你与一名在你攻击范围内的角色共计三张牌"
}

return hongjinxiuwei